
<!--
 * @Author: ljc
 * @Date: 2023-04-25 15:00:24
 * @LastEditors: g05047
 * @LastEditTime: 2023-05-10 11:17:37
 * @Description: 评审任务维护
-->

<template>
  <RkContainer :isFooter="false"
    :isAside=false>
    <template #header_left>
      <RkButtonGroup :options="json_review_button"
        v-model="queryParams.type"
        @click="handleSearch" />
    </template>
    <template #header_right>
      <el-button v-if="queryParams.type=='001'"
        @click="handleOperation('add')">新增任务</el-button>
      <el-button v-if="queryParams.type=='001'"
        @click="handleOperation('start','开始任务')">开始任务</el-button>
      <el-button v-if="queryParams.type=='001'"
        @click="handleOperation('edit','修改')">修改任务</el-button>
      <el-button v-if="queryParams.type=='001'"
        @click="handleOperation('assign','指派专家')">为项目指派专家</el-button>
      <el-button v-if="queryParams.type=='001' || queryParams.type=='004'"
        type="danger"
        @click="handleOperation('delete', '删除')">删除</el-button>
      <el-button v-if="queryParams.type=='002'"
        type="danger"
        @click="handleOperation('end','结束任务')">结束任务</el-button>
    </template>
    <template #main>
      <rk-condition>
        <template #content>
          <rk-grid ref="taskMangementRef"
            title="评审任务维护"
            dataUrl="/reviewtask/page"
            boxConfig
            :params="queryParams"
            :columns="getTableColumns()">
            <template #toolbar_title_buttons>
              <el-input v-model="queryParams.name"
                placeholder="请输入任务名称"
                class="mr-15px w-200px"
                clearable
                @click="handleSearch">
              </el-input>
            </template>
            <template #taskName="{ row }">
              <el-button type="primary"
                link
                @click="handleOperation('info','',row)">{{row.name}}</el-button>
            </template>
          </rk-grid>
        </template>
      </rk-condition>
    </template>
  </RkContainer>
  <task-management-edite-drawer ref="taskManagementEditeDrawerRef"
    @save="handleSearch"></task-management-edite-drawer>
  <project-expert-dialog ref="projectExpertRef"></project-expert-dialog>
  <project-expert-list-dialog ref="projectExpertListDialogRef"></project-expert-list-dialog>
  <project-task-list-dialog ref="projectTaskListDialogRef"></project-task-list-dialog>
  <task-management-detaile-dialog ref="taskManagementDetileDialogRef"></task-management-detaile-dialog>
</template>
  <script setup>

import { json_review_button } from '@/common/button-data.js'
import { COMMON_STOP_OPEN_ENUMS } from '@/enums/enums-common.js'
import { EXPERT_INPUT_STATUS } from '@/enums/enums-expert.js'
import { RkConfirmBox } from '@/components/message-box/index.js'
import { reviewtaskGetData, reviewtaskDeleteData, reviewtaskStartTaskData, reviewtaskStopTaskData } from '@/api/api-expert.js'
import TaskManagementEditeDrawer from './drawer/TaskManagementEditeDrawer.vue'
import ProjectExpertDialog from './dialogs/ProjectExpertDialog.vue'
import ProjectExpertListDialog from './dialogs/ProjectExpertListDialog.vue'
import ProjectTaskListDialog from './dialogs/ProjectTaskListDialog.vue'
import TaskManagementDetaileDialog from './dialogs/TaskManagementDetaileDialog.vue'
import { reactive } from 'vue'

const { appContext } = getCurrentInstance();
const globalProxy = appContext.config.globalProperties;
const taskMangementRef = ref(null)
const taskManagementEditeDrawerRef = ref(null)
const projectExpertRef = ref(null)
const projectExpertListDialogRef = ref(null)
const projectTaskListDialogRef = ref(null)
const taskManagementDetileDialogRef = ref(null)

// 查询参数
let queryParams = reactive({
  name: undefined,
  agId: undefined,
  type: '001',
})
const getTableColumns = () => {
  const tableColumns = [
    {
      field: 'name', title: '任务名称', width: 250, align: 'left', slots: { default: 'taskName' }
    },
    // {
    //   field: 'status', title: '启用状态', width: 120, align: 'center', slots: {
    //     default: ({ row }) => {
    //       const statusStr = `${row.status ?? 0}`
    //       return statusStr ? COMMON_STOP_OPEN_ENUMS.options[statusStr].label : ""
    //     }
    //   }
    // },
    {
      field: 'type', title: '类型', width: 120, align: 'center', slots: {
        default: ({ row }) => {
          return row.type ? EXPERT_INPUT_STATUS.options[row.type].label : ""
        }
      }
    },
    { field: 'createTime', title: '创建日期', width: 200, align: 'center', },
    { field: 'startDate', title: '开始日期', width: 200, align: 'center', },
    { field: 'endDate', title: '结束日期', width: 200, align: 'center', },
    { field: 'proNum', title: '项目总数', width: 120, align: 'center' },
    { field: 'expertNum', title: '专家总数', width: 120, align: 'center' },
    { field: 'remark', title: '备注', width: 120, align: 'left' },
  ]
  return tableColumns
}

/**查询 */
const handleSearch = () => {
  taskMangementRef.value.searchEvent()
}
//送审、撤销送审、删除
const handleOperation = async (buttonType, textName, row) => {
  if (buttonType === 'add') {
    taskManagementEditeDrawerRef.value.handleOpen()
  } else if (buttonType === 'edit') { //修改
    const seleRows = taskMangementRef.value.getCheckboxRecords()
    if (seleRows.length != 1 || !seleRows) {
      globalProxy.$message({ message: `请选择一条数据进行${textName}！`, type: "warning" });
      return;
    }
    const taskId = seleRows[0].taskId
    taskManagementEditeDrawerRef.value.handleOpen(taskId)
  } else if (buttonType == 'info') {
    const taskId = row.taskId
    taskManagementDetileDialogRef.value.handleOpen(taskId)
  } else if (buttonType == 'assign') { //为项目指派专家
    const seleRows = taskMangementRef.value.getCheckboxRecords()
    if (seleRows.length != 1 || !seleRows) {
      globalProxy.$message({ message: `请选择一条数据进行${textName}！`, type: "warning" });
      return;
    }
    const taskId = seleRows[0].taskId
    const taskName = seleRows[0].name
    projectExpertRef.value.handleOpen(taskId, taskName)
  } else {
    const seleRows = taskMangementRef.value.getCheckboxRecords()
    if (seleRows.length == 0 || !seleRows) {
      globalProxy.$message({ message: `请至少选择一条数据进行${textName}！`, type: "warning" });
      return;
    } else {
      RkConfirmBox({
        title: '提示',
        content: `您确定要${textName}吗？`,
        onSubmit: async function () {
          //获取选中数据
          const TaskId = []
          seleRows.forEach(element => {
            TaskId.push(element.taskId)
          });
          const taskIds = TaskId.join(',')
          if (buttonType == 'delete') {  //删除
            const params = { taskIds }
            const result = await reviewtaskDeleteData(params)
            const { data, code, msg } = result
            if (code === 200) {
              globalProxy.$message({ message: `${textName}成功！`, type: "success" });
              handleSearch()
            } else {
              globalProxy.$message({ message: `${msg}`, type: "error" });
            }
          } else if (buttonType == 'start') { //开始
            const params = { taskIds, processKey: queryParams.processKey }
            const result = await reviewtaskStartTaskData(params)
            const { data, code, msg } = result
            if (code === 200) {
              globalProxy.$message({ message: `${textName}成功！`, type: "success" });
              handleSearch()
            } else {
              globalProxy.$message({ message: `${msg}`, type: "error" });
            }
          } else if (buttonType == 'end') { //结束
            const params = { taskIds, processKey: queryParams.processKey }
            const result = await reviewtaskStopTaskData(params)
            const { data, code, msg } = result
            if (code === 200) {
              globalProxy.$message({ message: `${textName}成功！`, type: "success" });
              handleSearch()
            } else {
              globalProxy.$message({ message: `${msg}`, type: "error" });
            }
          }

        },
        onCancel: function () {
        }
      })
    }
  }
}
//新增项目
const handelEdit = (type) => {
  // if (!leaf.value) {
  //   globalProxy.$message({ message: '请先选择单位再进行操作！', type: "warning" });
  //   return;
  // }
  if (type === 'add') { // 新增任务

  } else if (type === 'edit') { // 修改任务
    // taskMangementRef.value.handleOpen()
    projectTaskListDialogRef.value.handleOpen()
  } else if (type === 'assign') {// 为项目指派专家
    projectExpertRef.value.handleOpen()
  } else if (type === 'start') {// 开始任务
    projectExpertListDialogRef.value.handleOpen()
  }
}

  </script>
  
  <style lang="scss" scoped>
.mr-15px {
  margin-right: 15px;
  &:last-child {
    margin-right: 0;
  }
}
.w-200px {
  width: 200px;
}
</style>
  